package com.mxt.moviedemo.service.impl;

import com.mxt.moviedemo.dao.ReviewsMapper;
import com.mxt.moviedemo.entity.Actors;
import com.mxt.moviedemo.entity.PageRequest;
import com.mxt.moviedemo.entity.PageResult;
import com.mxt.moviedemo.entity.Reviews;
import com.mxt.moviedemo.service.ReviewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ReviewsServiceImpl implements ReviewsService {
    @Autowired
    private ReviewsMapper reviewsMapper;
    @Override
    public List<Reviews> getReviewsByMovieId(int mmid) {
        return reviewsMapper.getReviewsByMovieId(mmid);
    }

    @Override
    public PageResult<Reviews> getReviewsByPage(PageRequest pageRequest) {
        List<Reviews> reviews = reviewsMapper.getReviewsByPage(pageRequest);
        // 过滤掉null元素
        List<Reviews> nonNullReviews = new ArrayList<>();
        for (Reviews review : reviews) {
            if (reviews != null) {
                nonNullReviews.add(review);
            }
        }
        int total = reviewsMapper.getTotalReviews();
        return new PageResult<>(pageRequest.getPage(), pageRequest.getSize(), total, nonNullReviews);
    }

    @Override
    public Reviews getReviewsById(int rid) {
        return reviewsMapper.getReviewsById(rid);
    }

    @Override
    public List<Reviews> searchReviewsByUid(int uid) {
        return reviewsMapper.searchReviewsByUid(uid);
    }

    @Override
    public int addReviews(Reviews reviews) {
        return reviewsMapper.addReviews(reviews);
    }

    @Override
    public boolean updateReviews(Reviews reviews) {
        return reviewsMapper.updateReviews(reviews);
    }

    @Override
    public int deleteReviews(int rid) {
        return reviewsMapper.deleteReviews(rid);
    }
}